﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class ConfigRepository : IConfigRepository
    {
        private mbEntities db;

        public ConfigRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public void UpdateConfig(string name, string value)
        {
            db.Configs.FirstOrDefault(x => x.Name == name).Value = value;
            db.SaveChanges();
        }

        public IEnumerable<Config> Configs()
        {
            return db.Configs.AsEnumerable();
        }

        public string Value(string ConfigName)
        {
            return db.Configs.FirstOrDefault(x => x.Name == ConfigName).Value;
        }

        public bool IsConfig(string ConfigName)
        {
            return db.Configs.ToList().Exists(x => x.Name == ConfigName);
        }

        public void BulkUpdate(Dictionary<string, string> Vals)
        {
            foreach (var v in Vals)
            {
                db.Configs.First(x => x.Name == v.Key).Value = v.Value;
            }
            db.SaveChanges();
        }
    }
}
